import config from '@/config/config.js';
import axios from 'axios';

const workshopRequest = {
    loadWorkshopList(searchWorkshop, callback){
        const url = `${config.mdBaseUrl}/workshop/query`;

        axios.get(url, {
            params: {
                workshopName: searchWorkshop.workshopName || null,
                workshopCode: searchWorkshop.workshopCode || null,
                workshopId: searchWorkshop.workshopId || null,
            },
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // code 字段来标识成功状态
                callback(res.data);
            }
        })
    },
    deleteWorkshopList(workshopId, callback){
        const url = `${config.mdBaseUrl}/workshop/delete?workshopId=${workshopId}`;

        axios.delete(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // code 字段来标识成功状态
                callback(res.data);
            }else if (res.data.msg == "OK"){
                callback(res.data);
            }
        })
    },
    loadWorkshopById(workshopId, callback){
        const url = `${config.mdBaseUrl}/workshop/queryById?workshopId=${workshopId}`;

        axios.get(url, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // code 字段来标识成功状态
                callback(res.data);
            }else if (res.data.msg == "OK"){
                callback(res.data);
            }
        })
    },
    updateWorkshop(workshop, callback){
        const url = `${config.mdBaseUrl}/workshop/update`;
        axios.post(url, workshop, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // code 字段来标识成功状态
                callback(res.data);
            }else if (res.data.msg == "OK"){
                callback(res.data);
            }else {
                console.error('请求失败:',res.data.message);
            }
        })
    },
    insertWorkshop(workshop, callback){
        const url = `${config.mdBaseUrl}/workshop/insert`;
        axios.post(url, workshop, {
            headers: {
                token: localStorage.getItem("token") // 传递token进行身份验证
            }
        }).then(res => {
            if (res.data && res.data.code === 200) {  // code 字段来标识成功状态
                callback(res.data);
            }else if (res.data.msg == "OK"){
                callback(res.data);
            }else {
                console.error('请求失败:',res.data.message);
            }
        })
    }


}

export default workshopRequest;